#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using ISecurityPolicy = PHSRAG.Insight21.Policy.ISecurityPolicy;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	/// <summary>
	/// Summary description for ExceptionReport.
	/// </summary>
	public class ExceptionReport  : BO.BusinessObject
	{
		#region Constants
		private const string ExceptionReportKey = "ExceptionReport_F38408D2-3F6C-43A6-A97C-B22DB0703292";
		private const bool		AscendingSortSequence	= true;
		private const string ExceptionReportTableName = "ExceptionReport";
		#endregion

		# region Instance Variables
		private bool			sortAscending= AscendingSortSequence;
		private string			sortColumn="EmployeeName";
		# endregion

		#region Constructor
		/// <summary>
		/// Initialize the state of this object
		/// </summary>
		public ExceptionReport(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet();
		}
	#endregion

		# region class Methods
		public static ExceptionReport GetCachedObject()
		{
			return CachePolicy.GetCachedObject(ExceptionReportKey) as ExceptionReport;;
		}
		#endregion

		# region Public Methods
		public void SortAndFilter(DataView dataView, params object[] args)
		{

			dataView.Table = DataContainer.Tables[ExceptionReportTableName];
			dataView.Sort = String.Format("{0} {1}",sortColumn, sortAscending ? "ASC" : "DESC");
			//Create filter criteria from arguments passed.
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			if (args[0] != System.DBNull.Value && ((string)args[0]).Trim()!="")
				sb.AppendFormat(" and (EffectiveDate >= '{0}')", args[0]);
			
			if (args[1] != System.DBNull.Value && ((string)args[1]).Trim()!="")
				sb.AppendFormat(" and (EffectiveDate <= '{0}')", args[1]);

			if (args[2] != System.DBNull.Value && ((string)args[2]).Trim()!="")
				sb.AppendFormat(" and (ExpirationDate >= '{0}')", args[2]);
			
			if (args[3] != System.DBNull.Value && ((string)args[3]).Trim()!="")
				sb.AppendFormat(" and (ExpirationDate <= '{0}')", args[3]);

			if (sb.Length > 0)
				dataView.RowFilter = sb.ToString().Substring(5,sb.Length-5);


		}

		public void ProcessSortCriteria(string columnName)
		{
			sortAscending = (sortColumn == columnName) ? !sortAscending : AscendingSortSequence;
			sortColumn = columnName;
		}
		# endregion

		#region Business Object Methods
		public override void Cache()
		{
			CachePolicy.Cache(ExceptionReportKey, this);
		}

		public override void UnCache()
		{
			CachePolicy.UnCache(ExceptionReportKey);
		}

		/// <summary>
		/// Load the DataSet with Salary Grid data by executing the stored procedure
		/// "GetSalaryGrid".  Throws system exception if an error is encountered 
		/// during the load process.
		/// </summary>
		/// <param name="connectionString">Sql Connection string info</param>
		public override void Load(string connectionString, params object[] args)
		{
			DataContainer.Reset();

			Utility.DataAccess.LoadDataSet(DataContainer, "ExceptionReport", connectionString, "TestGetExceptions",
					Utility.DataAccess.BuildSqlParameterArray(
							"@ExceptionDate",SqlDbType.DateTime,Convert.ToDateTime(args[0])));
			
		}
		#endregion

		#region Private Methods
		private void GenerateExceptionMessage(){}
		#endregion
		
	}
}
